$(function () {
    layui.use(["laypage", "table", "form"], function () {
        var laypage = layui.laypage;
        var page, limit;
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var element = layui.element;
        form.render();
        table.render({
            elem: '#tb',
            url: '../user/findAll',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板

            defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            title: '通讯录数据表',
            height: 750,
            cols: [[
                {
                    type: 'checkbox',
                    fixed: 'left'
                },
                {
                    field: 'u_name',
                    title: '姓名',
                    width: 300,
                    sort: true,
                    fixed: true
                }, {
                    field: 'u_phone',
                    title: '手机',
                    width: 300
                }, {
                    field: 'u_sex',
                    title: '性别',
                    width: 300,
                    sort: true,
                }, {
                    field: 'u_department',
                    title: '部门',
                    width: 300
                }, {
                    field: 'u_job',
                    title: '岗位',
                    width: 300
                },
                {
                    fixed: 'right',
                    title: '操作',
                    minWidth: 100,
                    align: 'center',
                    toolbar: '#barDemo'
                }
            ]
            ],
            // data: [{
            //     "id": 1
            //     , "my_attention": '<i class="layui-icon layui-icon-star" flag=1 style="font-size:20px;"></i>'
            //     , "my_name": "李四"
            //     , "my_phone": "123434"
            //     , "my_department": "人事部"
            //     , "my_job": "经理"
            // }, {
            //     "id": 2
            //     , "my_attention": '<i class="layui-icon layui-icon-star" flag=1 style="font-size:20px;"></i>'
            //     , "my_name": "王五"
            //     , "my_phone": "23244"
            //     , "my_department": "彻底的"
            //     , "my_job": "但是"
            // }],
            page: true,
            id: 'testReload',
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                }
            }

        });

        //监听表格操作
        table.on('tool(cu_demo)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            var tr = obj.tr;
            console.log(layEvent)


            if (layEvent === 'no_pass') { //编辑
                $.ajax({
                    url: '../focus/saveFocus',
                    type: 'get',
                    data: {
                        'fo_be_id': data.id,
                        'fo_u_id': sessionStorage.getItem("userid")
                    },
                    success: function (suc) {
                        if (suc == 'success') {

                            $("#pass").remove()
                            console.log($("#pass").remove())
                            window.location.reload();
                        } else {
                            layer.msg("审核通过", {
                                icon: 5
                            });
                            $("#pass").remove()
                            console.log($("#pass").remove())
                        }
                    }
                });

            }
        });


        form.on('select(cu_select)', function (data) {
            // console.log(data);
            var u_department = data.value;
            if (u_department == '所有部门') {
                console.log(u_department);
                table.reload('testReload', {
                    url: '../user/findAll',
                    page: {
                        curr: 1
                    }
                })
            } else {
                table.reload('testReload', {
                    url: '../user/findByDepartment',
                    page: {
                        curr: 1
                    },
                    where: {
                        u_department: u_department
                    }
                })

            }
        });


        //监听表格复选框选择
        $('table').on('click', 'i', function (obj) {
            var flag1 = $(this).attr("flag");
            if (flag1 == 1) {
                $(this).attr("class", "layui-icon layui-icon-star-fill");
                // console.log($(this).attr("class"));
                $(this).css("color", "#fac23d");
                $(this).attr("flag", 0);
            } else {
                $(this).attr("class", "layui-icon layui-icon-star");
                $(this).css("color", "black");
                $(this).attr("flag", 1);
            }

            console.log($(this).attr("flag"))
        });
        /* 	//监听表格复选框选择
            table.on('checkbox(demo)', function(obj) {
                console.log(obj)
            }); */
        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;

            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.alert('编辑行：<br>' + JSON.stringify(data))
            }
        });


        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            // active[type] ? active[type].call(this) : '';
            console.log(type)
        });

        //7.搜索按钮
        $('#search_btn').click(function () {
            var inputVal = $('#ad_input').val();
            console.log(inputVal);
            if (inputVal == '' || inputVal == null) {
                table.reload('testReload', {
                    url: '../user/findAll',
                    //methods:"post"
                    where: {
                        u_name: inputVal //设置需要传递的参数

                    },
                    page: {
                        curr: 1 //条件查询从第一页开始，否则从当前页面开始
                    }
                });
            } else {
                table.reload('testReload', {
                    url: '../user/findByName',
                    //methods:"post"
                    where: {
                        u_name: inputVal //设置需要传递的参数

                    },
                    page: {
                        curr: 1 //条件查询从第一页开始，否则从当前页面开始
                    }
                });
            }

        });

    })
})
